Gallery.views.LoginCard = function()
{
    var cfg = {
        id:"fuckingForm",
        fullscreen:true,
        dockedItems:[{
            dock : "top",
            xtype : "toolbar",
            title : "Login"
        }],
        items: [
		{
		xtype: 'fieldset',
		title: 'Config',
		items: [{
				xtype: 'textfield',
				name : 'url',
				label: 'URL',
				value: "http://192.168.1.100:8080/oobdoo"
				}]
		},
        {
            xtype: 'fieldset',
            title: 'Account',
            items: [{
                xtype: 'textfield',
                name : 'lg',
                label: 'Username',
					value: "xenogaz"
            },{
                xtype: 'passwordfield',
                name : 'pw',
                label: 'Password',
					value: "qsdqsd"
            }]
        },{
            xtype:  'button',
            text:   'Log in',
            ui:     'confirm',
            handler: this.submit,
            scope: this
        }]
    };
    Gallery.views.LoginCard.superclass.constructor.call(this, cfg);
};

Gallery.views.Login = Ext.extend(Gallery.views.LoginCard, Ext.form.FormPanel, {
    submit: function(){
		Gallery.viewport.addDocked([topToolbar]);
        Gallery.viewport.setLoading(true,false);
        
        var formValues = this.getValues();
								 
		Gallery.urlserver = formValues['url'];
								 console.log("LOGIN/ url server :"+Gallery.urlserver);
		if(Gallery.urlserver != "")
		{
    
        var param = new Object();
        param.lg = formValues['lg'];
        param.pw = formValues['pw'];
        param.action = "login";
        
        Ext.util.JSONP.request({
            url: Gallery.urlserver+'/api',
            callbackKey: 'callback',
            params: param,
			scope: this,
            callback: function(response)
            {
                console.log('FUUUUUUUUUUUUU');
                //var t = eval("("+response.responseText+")");
                var t = response;
                            console.log(t);
                            if(t.ret.responseCode == "0")
                            {
                                // no bug return , we can log user
                                console.log("in");
                                var index = Gallery.CommonStore.findExact('key','uk');
                                var record;
                                if(index == -1)
                                {
                                    //unique key doesnt exist
                                    console.log('insert');
                                    Gallery.CommonStore.add({
                                        key: 'uk',
                                        value: t.ret.uuid
                                    });
                                }
                                else
                                {
                                    // unique key exist
                                    console.log("update uk");
                                    record = Gallery.CommonStore.getAt(index);
                                    record.data.value = t.ret.uuid;
                                }

                                index = Gallery.CommonStore.findExact('key','lg');

                                if(index == -1)
                                {
                                    console.log('insert');
                                    Gallery.CommonStore.add({
                                        key: 'lg',
                                        value: param.lg
                                    });
                                }
                                else
                                {
                                    console.log("update login");
                                    record = Gallery.CommonStore.getAt(index);
                                    record.data.value = param.lg;
                                }

                                Gallery.CommonStore.sync(); 

                                Gallery.logged = true;
                                Gallery.useruuid = t.ret.uuid;
                                Gallery.userlogin = param.lg;



                                Gallery.viewport.setLoading(false);

                                Ext.getCmp("fuckingForm").hide();
                                Ext.redirect("Album/index");
                            }
                            else
                            {
                                Ext.Msg.alert(t.ret.responseMsg, data.errorMessage, Ext.emptyFn);
                                Gallery.viewport.setLoading(false);
                            }
            }
        });
								 
		}
        
        /*
        Ext.Ajax.request({
            url: 'http://127.0.0.1:8080/oobdoo/api',
            params: param,
            success: function(response, opts) {
                
                var t = eval("("+response.responseText+")");
                console.log(t);
                if(t.ret.responseCode == "0")
                {
                    // no bug return , we can log user
                    console.log("in");
                    var index = Gallery.CommonStore.findExact('key','uk');
                    var record;
                    if(index == -1)
                    {
                        //unique key doesnt exist
                        console.log('insert');
                        Gallery.CommonStore.add({
                            key: 'uk',
                            value: t.ret.uuid
                        });
                    }
                    else
                    {
                        // unique key exist
                        console.log("update uk");
                        record = Gallery.CommonStore.getAt(index);
                        record.data.value = t.ret.uuid;
                    }
                    
                    index = Gallery.CommonStore.findExact('key','lg');
 
                    if(index == -1)
                    {
                        console.log('insert');
                        Gallery.CommonStore.add({
                            key: 'lg',
                            value: param.lg
                        });
                    }
                    else
                    {
                        console.log("update login");
                        record = Gallery.CommonStore.getAt(index);
                        record.data.value = param.lg;
                    }
 
                    Gallery.CommonStore.sync(); 
                    
                    Gallery.logged = true;
                    Gallery._useruuid = t.ret.uuid;
                    Gallery._userlogin = param.lg;
            
                   

                    Gallery.viewport.setLoading(false);
                    
                    Ext.getCmp("fuckingForm").hide();
                    Ext.redirect("Album/index");
                }
                else
                {
                    Ext.Msg.alert(t.ret.responseMsg, data.errorMessage, Ext.emptyFn);
                    Gallery.viewport.setLoading(false);
                }
                
                
            }
        });
        */
    }
});

Ext.reg('login', Gallery.views.Login);

